
var app = getApp()
Page({
  data: {
    setCount: [{ name: "男" }, { name: "女" }],
    count: -1,
    froms:{
      name:'',
      sex:'',
      phone:'',
      email:'',
      company:'',
      position:'',
      address:'',
      birthday:'',
      is_mask:false,
      remark:''
    }
  },
  shuaxin:function(e){
    let that = this
    app.util.request({
      'url': 'entry/wxapp/clientInfo',
      'cachetime': '30',
      'showLoading': false,
      'method': 'POST',
      'data': {
        client_id: that.data.id
      },
      success: function (res) {
        // console.log("entry/wxapp/formid ==>", res)
        if (!res.data.errno) {
          // console.log(res)
          let aa = res.data.data.info
          for (let i in aa) {
            if (aa[i] == 'undefined') {
              aa[i] = ''
            }
          }
          console.log(that.data.froms)
          that.setData({
            'froms.name': aa.name,
            'froms.sex': aa.sex,
            count: aa.sex,
            'froms.phone': aa.phone,
            'froms.email': aa.email,
            'froms.company': aa.company,
            'froms.position': aa.position,
            'froms.address': aa.address,
            'froms.birthday': aa.birthday,
            'froms.is_mask': aa.is_mask,
            'froms.remark': aa.remark,
          })
          e ? wx.stopPullDownRefresh() : ''
        }
      }
    })
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数 
    var that = this;
    if (options.id){
      this.setData({
        id: options.id
      })
    }else{
      this.setData({
        id: '471'
      })
    }
    this.shuaxin()
  },
  onReady: function () {
    // console.log("页面渲染完成")
  },
  onShow: function (e) {
    // 页面显示 
  },
  onHide: function () {
    // console.log("页面隐藏")
  },
  onUnload: function () {
    // console.log("页面关闭")
  },
  onPullDownRefresh: function () {
    // console.log("监听用户下拉动作")  
    this.shuaxin(1)
  },
  onReachBottom: function () {
    // console.log("监听页面上拉触底")  
  },
  onPageScroll: function (e) {
    //console.log("监听页面滚动", e);
  },
  onShareAppMessage: function (res) {
    // console.log("用户点击右上角分_享")
    var that = this;
  },
  pickerSelected: function (e) {
    let that = this;
    var value = e.detail.value;
    if(value){
      that.setData({
        count: value,
        'froms.sex': value
      })
    }
    // console.log(this.data.froms)
  },
  blur_name: function (e) {
   
    let val = e.detail.value
    if (val) {
      this.setData({
        'froms.name': val
      })
    }
    //  console.log(this.data.froms)
  },
  blur_phone: function (e) { 
    
    let val = e.detail.value
    
    if (val) {
      // console.log('e')
      let myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
      if (myreg.test(val)) {
        this.setData({
          'froms.phone': val
        })
      } else {
        wx.showToast({
          title: '请填写正确电话号码！'
        })
      }
    }
    
    // console.log(this.data.froms)
  },
  blur_email: function (e) { 
    let val = e.detail.value
    if (val) {
    let myreg = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;
    if (myreg.test(val)) {
      this.setData({
        'froms.email': val
      })
    } else {
      wx.showToast({
        title: '请填写正确邮箱！'
      })
    }
    }
  },
  blur_company: function (e) {
    let val = e.detail.value
    if (val) {
      this.setData({
        'froms.company': val
      })
    }
    
  },
  blur_position: function (e) {
    let val = e.detail.value
    if (val) {
    this.setData({
      'froms.position': val
    })
    }
   },
  blur_address: function (e) { 
    let val = e.detail.value
    if (val) {
    this.setData({
      'froms.address': val
    })
    }
  },
  blur_birthday:function(e){
    let val = e.detail.value
    if (val) {
    this.setData({
      'froms.birthday': val
    })
    }
  },
  switchChange: function (e) {
    let val = e.detail.value
    if (val) {
    this.setData({
      'froms.is_mask': e.detail.value
    })
    }
    // console.log('switch1 发生 change 事件，携带值为', e.detail.value)
  }, 
  blur_Remarks:function(e){
    
    let val = e.detail.value
    if (val) {
    // console.log(val)
    this.setData({
      'froms.remark': val
    })
    }
    //  console.log(this.data.froms.remark)
  },
  adds: function (e) {
    let that = this
    let datas = that.data.froms
    datas.client_id = that.data.id
    // console.log(datas)
    // console.log(datas)
   
    app.util.request({
      'url': 'entry/wxapp/editClient',
      'cachetime': '30',
      'showLoading': false,
      'method': 'POST',
      'data': datas,
      success: function (res) {
        // console.log("entry/wxapp/formid ==>", res)
        if (!res.data.errno) {
          wx.showToast({
            title: '保存成功',
            success:function(){
              setTimeout(function(){
                wx.navigateBack({
                  delta: 1
                })
              },500)
            }
          })
        }
      }
    })
   },
})